<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template #state="{row}">
        <el-tag v-if="row.state === 'SUCCESS'" type="success">成功</el-tag>
        <el-tag v-else-if="row.state === 'SUSPECTED_DUPLICATION'" type="warning">疑似重复</el-tag>
        <el-tag v-else-if="row.state === 'CANCELLATION'" type="info">作废</el-tag>
        <el-tag v-else type="danger">未知</el-tag>
      </template>
      <!-- 操作-通过-作废 -->
      <template #menu="{row,index,size}">
        <div v-if="permission.report_auth && row.state === 'SUSPECTED_DUPLICATION'">
          <el-button
            type="success"
            icon="el-icon-delete"
            :size="size"
            link
            @click="handleState(row.id,'SUCCESS')">通过
          </el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            :size="size"
            link
            @click="handleState(row.id,'CANCELLATION')">作废
          </el-button>
        </div>
      </template>
      <!-- 操作-删除 -->
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.report_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <!-- 弹窗-保存-取消 -->
      <template #menu-form="{row,index,type}">
        <el-button type="primary"
                   icon="el-icon-circle-plus"
                   v-if="type==='add'"
                   @click="$refs.crud.rowSave()">报备
        </el-button>
        <el-button icon="el-icon-circle-close"
                   @click="$refs.crud.closeDialog()">取消
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { add, getDetail, getList, remove, update } from '@/api/business/report';
import { mapGetters } from 'vuex';
import { dept } from '@/api/system/dept';

const stateDicData = [
  { label: '成功', value: 'SUCCESS' },
  { label: '疑似重复', value: 'SUSPECTED_DUPLICATION' },
  { label: '作废', value: 'CANCELLATION' }
];

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        gridBtn: false,
        columnBtn: false,
        searchShowBtn: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        selection: false,
        saveBtn: false,
        cancelBtn: false,
        column: [
          {
            label: '名称',
            prop: 'name',
            search: true,
            rules: [{
              required: true,
              message: '请输入名称',
              trigger: 'blur'
            }]
          },
          {
            label: '建设单位',
            prop: 'projectParty',
            search: true,
            rules: [{
              required: true,
              message: '请输入建设单位',
              trigger: 'blur'
            }]
          },
          {
            label: '公司',
            prop: 'createDept',
            dataType: 'string',
            type: 'select',
            addDisplay: false,
            editDisplay: false,
            dicUrl: dept.deptListUrl,
            props: {
              label: 'deptName',
              value: 'id'
            },
            rules: [{
              required: true,
              message: '请输入公司',
              trigger: 'blur'
            }]
          },
          {
            label: '状态',
            prop: 'state',
            type: 'select',
            search: true,
            addDisplay: false,
            editDisplay: false,
            dicData: stateDicData,
            props: {
              label: 'label',
              value: 'value'
            },
            rules: [{
              required: true,
              message: '请选择状态',
              trigger: 'blur'
            }]
          },
          // {
          //   label: '共享状态',
          //   prop: 'shareState',
          //   type: 'select',
          //   clearable: false,
          //   dicData: [
          //     { label: '私有', value: 'PRIVATE' },
          //     { label: '共享', value: 'PUBLIC' }
          //   ],
          //   props: {
          //     label: 'label',
          //     value: 'value'
          //   },
          //   rules: [{
          //     required: true,
          //     message: '请选择共享状态',
          //     trigger: 'blur'
          //   }]
          // },
          {
            label: '开标日期',
            prop: 'signingDate',
            type: 'date',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            rules: [{
              required: true,
              message: '请选择开标日期',
              trigger: 'blur'
            }]
          },
          {
            label: '报备时间',
            prop: 'createTime',
            type: 'date',
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss'
          },
          {
            label: '项目简况',
            prop: 'explanation',
            type: 'textarea',
            dataType: 'string',
            span: 12,
            minRows: 5,
            hide: true,
            maxlength: 300,
            showWordLimit: true
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission', 'userInfo']),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.report_add, false),
        viewBtn: this.validData(this.permission.report_view, false),
        delBtn: this.validData(this.permission.report_delete, false),
        editBtn: this.validData(this.permission.report_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    handleState(id, state) {
      const params = { id, state };
      update(params).then((res) => {
        // 刷新列表
        this.onLoad(this.page, this.query);
      });
    },
    rowSave(row, done, loading) {
      const createDept = this.userInfo.deptId;
      if (this.validatenull(createDept)) {
        this.$message.error('公司信息失败，请刷新页面');
        return;
      }

      row.createDept = createDept;
      row.state = null;
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = Number(data.total);
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
